/*
 * @Descripttion:
 * @version:
 * @Author: ChenXiang
 * @Date: 2021-03-22 16:18:10
 * @LastEditors: ChenXiang
 * @LastEditTime: 2021-09-28 11:05:30
 */

// const ua: string = window.navigator.userAgent;

// const isWeixin: boolean = /MicroMessenger/i.test(ua);
// const isAndroid: boolean = /Android/i.test(ua);
// const isIOS: boolean = /iP[hone|ad|od] OS/i.test(ua);
// const isIphone: boolean = /iPhone/i.test(ua);

// // iphoneX iphoneXS 刘海高度 30px
// const isIphoneX: boolean = !!(isIphone && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 375 && window.screen.height === 812);

// // 刘海高度： 44px
// const isIphoneXSMAX: boolean = !!(isIphone && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 414 && window.screen.height === 896);

// // 刘海高度 33px
// const isIphoneXR: boolean = !!(isIphone && window.devicePixelRatio && window.devicePixelRatio === 2 && window.screen.width === 414 && window.screen.height === 896);

// // iPhoneX版本以上的刘海屏
// const isIphoneXup: boolean = isIphoneX && isIphoneXSMAX && isIphoneXR;

const UA = navigator.userAgent;
const ua = navigator.userAgent.toLowerCase();
const canUseDOM = !!(
  typeof window !== "undefined" &&
  window.document &&
  window.document.createElement
);
export default {
  IS_IOS: canUseDOM && /iphone|ipad|ipod/i.test(window.navigator.userAgent),

  // 判断ios是否大于13.3 (13,3)
  IOSVersion: (big, small) => {
    const ver = navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/);
    return ver && ver.length > 3 && ver[1] >= big && ver[2] >= small;
  },
  isIphonex() {
    if (typeof window !== "undefined" && window) {
      return (
        /iphone/gi.test(window.navigator.userAgent) &&
        window.screen.height >= 812
      );
    }
    return false;
  },

  /**
   * 比较两个给定的版本号（x.y.z形式，符合semver规范），
   * va大于vb返回1，va小于vb返回-1，相等返回0
   * 如果需要更复杂的比较，可以使用semver包比较
   * @param {*} va
   * @param {*} vb
   * @example compareVersion("2", "2.9.3")=>-1
   * @example compareVersion("2.9.3", "2.9.3")=>0
   * @example compareVersion("2.9.4", "2.9.3")=>1
   */
  compareVersion: function (va, vb) {
    const v1 = va.split(".");
    const v2 = vb.split(".");
    return (
      this.compare(v1[0], v2[0]) ||
      this.compare(v1[1], v2[1]) ||
      this.compare(v1[2], v2[2])
    );
  },
  compare(a, b) {
    // to number
    if (a && b) {
      a = +a;
      b = +b;
    }
    return a && !b ? 1 : b && !a ? -1 : a < b ? -1 : a > b ? 1 : 0;
  },

  // 是否在支付宝容器中打开
  isAli: UA.indexOf("AlipayClient") > -1 || UA.indexOf("AliApp(AP") > -1,
  // 分辨设备是移动设备还是桌面设备
  detectDeviceType: () =>
    /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
      navigator.userAgent
    )
      ? "Mobile"
      : "Desktop",

  // 是否在微信
  isWx: /MicroMessenger/.test(UA),

  // 是否Android
  isAndroid: /Android/.test(UA),

  isIos: /iPhone/.test(UA),

  /**
   * 判断安卓版本
   * @return 返回安卓版本，非安卓返回null
   * @example 4.2.2
   */

  // 确定页面的浏览器选项卡是否处于前台活跃状态
  isBrowserTabFocused: () => !document.hidden,

  androidVersion: () => {
    let version = null;
    if (ua.indexOf("android") > 0) {
      const reg = /android [\d._]+/gi;
      const vInfo = ua.match(reg);
      version = (vInfo + "").replace(/[^0-9|_.]/gi, "").replace(/_/gi, ".");
    }
    return version;
  },

  /**
   * 判断浏览器是否是无痕模式
   * @return Promise
   */
  browserIsVisible: (): Promise<boolean | undefined> => {
    return new Promise((resolve, reject) => {
      window.webkitStorageInfo.queryUsageAndQuota(
        window.webkitStorageInfo.TEMPORARY,
        function (used: number, remaining: number) {
          if (remaining < 120000000) {
            resolve(true);
            console.log("无痕模式");
          } else {
            resolve(false);
            console.log("非无痕模式");
          }
        },
        function (e: any) {
          reject(new Error(undefined));
          console.log("Error", e);
        }
      );
    });
  },
  // 是否是QQ浏览器
  isQQBrowser: () => !!ua.match(/mqqbrowser|qzone|qqbrowser|qbwebviewtype/i),
  // 是否是爬虫
  isSpider: () =>
    /adsbot|googlebot|bingbot|msnbot|yandexbot|baidubot|robot|careerbot|seznambot|bot|baiduspider|jikespider|symantecspider|scannerlwebcrawler|crawler|360spider|sosospider|sogou web sprider|sogou orion spider/.test(
      ua
    ),
};
